package com.gxa.service.admin.impl;

import com.gxa.common.R.ResponseData;
import com.gxa.mapper.admin.AdminMapper;
import com.gxa.pojo.*;
import com.gxa.service.admin.AdminService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.security.Principal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public ResponseData select_company() {
        ResponseData responseData = new ResponseData();
        XiTong xiTong = adminMapper.select_company();
        responseData.setCode(200);
        responseData.setMsg("进入成功");
        responseData.setData(xiTong);

        return responseData;
    }

    @Override
    public ResponseData update_company(HttpServletRequest request,String xiTongName, String website, String qiYeName, String qiYeAbstract,
                                       String phone, int onOff, int timeout, int wrongNum, MultipartFile[] logoPic) throws IOException, SQLException {
        ResponseData responseData = new ResponseData();
        MultipartFile file01=logoPic[0];
        if (file01.isEmpty()){
            System.out.println("未进行图片修改");
        }else {
            System.out.println("开始上传");
            System.out.println("request.getServletContext().getRealPath(\"\")="+request.getServletContext().getRealPath(""));
            String uploadRes="upload"+ File.separator;
            String uploadPath = request.getServletContext().getRealPath("") + File.separator + uploadRes + File.separator;   // 得到相当项目工程的硬盘物理地址
            String fileName = file01.getOriginalFilename();

            File newFile = new File(uploadPath + fileName);         // 根据当前工程的物理路径+文件名生成一个file对象
            System.out.println("newFile=" + newFile);
            FileCopyUtils.copy(file01.getBytes(), newFile);

            // 更新数据库
            adminMapper.updateHead(File.separator + uploadRes + File.separator + fileName);
        }

        int i=adminMapper.update_company(xiTongName,website,qiYeName,qiYeAbstract,phone,onOff,timeout,wrongNum);
        if (i>=1){
            responseData.setCode(200);
            responseData.setMsg("修改成功");
            return responseData;
        }else {
            responseData.setCode(506);
            responseData.setMsg("修改失败");
            return responseData;
        }
    }

    @Override
    public ResponseData select_log() {
        ResponseData responseData = new ResponseData();
        List<BackupLog> backupLog = adminMapper.select_log();
        responseData.setCode(200);
        responseData.setMsg("进入成功");
        responseData.setData(backupLog);

        return responseData;
    }

    @Override
    public ResponseData insert_log(Principal user, String date, String size, String handle) {
        ResponseData responseData = new ResponseData();
        String s = user.toString();
        adminMapper.insert_log(s,date,size,handle);
        BackupLog backupLog = new BackupLog();
        backupLog.setBackupName(s);
        backupLog.setBackupTime(date);
        backupLog.setLogSize(size);
        backupLog.setHandle(handle);

        responseData.setCode(200);
        responseData.setMsg("进入成功");
        responseData.setData(backupLog);

        return responseData;
    }

    @Override
    public ResponseData select_variable(int currentPage, int pageSize, String keyWord) {
        int index=(currentPage-1)*pageSize;
        List<BianLiang> bianLiang = adminMapper.select_variable(keyWord, pageSize, index);
        int sum = adminMapper.selectAllCount(keyWord);
        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        responseData.setMsg("进入成功");
        responseData.setData(bianLiang);
        responseData.setTotal(sum);
        return responseData;
    }

    @Override
    public ResponseData into_update_variable(int id) {
        ResponseData responseData = new ResponseData();
        BianLiang bianLiang=adminMapper.into_update_variable(id);
        responseData.setCode(200);
        responseData.setMsg("进入成功");
        responseData.setData(bianLiang);
        return responseData;
    }

    @Override
    public ResponseData update_variable(int id, int bianHao, String message, String title, String value) {
        ResponseData responseData = new ResponseData();
        adminMapper.update_variable(id,bianHao,message,title,value);
        responseData.setCode(200);
        responseData.setMsg("修改成功");
        return responseData;
    }

    @Override
    public ResponseData select_role(int currentPage, int pageSize, String keyWord) {
        int index=(currentPage-1)*pageSize;
        ResponseData responseData = new ResponseData();
        List<Role> roleList = adminMapper.select_role(keyWord, pageSize, index);
        int i = adminMapper.selectAllRole(keyWord);
        responseData.setCode(200);
        responseData.setMsg("进入成功");
        responseData.setData(roleList);
        responseData.setTotal(i);
        return responseData;
    }

    @Override
    public ResponseData into_add_role() {
        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        responseData.setMsg("进入成功");
        return responseData;
    }

    @Override
    public ResponseData add_role(String rolename, String note, String perm) {
        ResponseData responseData = new ResponseData();
        String[] split = perm.split("|");
        adminMapper.add_role(rolename,note);
        int newId=adminMapper.findNewRoleId(rolename);
        for (String s: split) {
            int i = Integer.parseInt(s);
            adminMapper.add_roleMenu(i,newId);
        }
        responseData.setCode(200);
        responseData.setMsg("新增成功");
        return responseData;
    }

    @Override
    public ResponseData into_update_role(int id) {
        ResponseData responseData = new ResponseData();
        List<Role> roleList = adminMapper.select_update_role(id);
        responseData.setCode(200);
        responseData.setMsg("进入成功");
        responseData.setData(roleList);
        return responseData;
    }

    @Override
    public ResponseData update_role(int id,String rolename, String note, String perm) {
        ResponseData responseData = new ResponseData();
        adminMapper.deleteRoleMenu(id);
        String[] split = perm.split("|");
        for (String s: split) {
            int i = Integer.parseInt(s);
            adminMapper.add_roleMenu(i,id);
        }
        adminMapper.update_role(id,rolename,note);
        responseData.setCode(200);
        responseData.setMsg("修改成功");
        return responseData;
    }

    @Override
    public ResponseData select_user(int currentPage, int pageSize, String keyWord) {
        int index=(currentPage-1)*pageSize;
        ResponseData responseData = new ResponseData();
        List<User> list= adminMapper.select_user(keyWord, pageSize, index);
        int sum = adminMapper.selectAllUser(keyWord);
        responseData.setCode(200);
        responseData.setMsg("进入成功");
        responseData.setData(list);
        responseData.setTotal(sum);
        return responseData;
    }

    @Override
    public ResponseData into_add_user() {
        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        responseData.setMsg("进入成功");
        return responseData;
    }

    @Override
    public ResponseData add_user(String number, String realname, String password, String sex, String rolename, String phone, String email) {
        ResponseData responseData = new ResponseData();
        Md5Hash hash = new Md5Hash(password, "?zk", 1024);
        String newPassWord = hash.toString();
        adminMapper.add_user(number,realname,newPassWord,sex,rolename,phone,email);
        responseData.setCode(200);
        responseData.setMsg("新增成功");
        return responseData;
    }

    @Override
    public ResponseData into_update_user(int id) {
        ResponseData responseData = new ResponseData();
        User user=adminMapper.into_update_user(id);
        responseData.setCode(200);
        responseData.setMsg("进入成功");
        responseData.setData(user);
        return responseData;
    }

    @Override
    public ResponseData update_user(int id,String number, String realname, String password, String sex, String rolename, String phone, String email) {
        ResponseData responseData = new ResponseData();
        Md5Hash hash = new Md5Hash(password, "?zk", 1024);
        String newPassWord = hash.toString();
        adminMapper.update_user(id,number,realname,newPassWord,sex,rolename,phone,email);
        responseData.setCode(200);
        responseData.setMsg("修改成功");
        return responseData;
    }

    @Override
    public ResponseData roleNameList() {
        ResponseData responseData = new ResponseData();
        List<Role> roleList = adminMapper.roleNameList();
        responseData.setCode(200);
        responseData.setMsg("成功");
        responseData.setData(roleList);
        return responseData;
    }

    @Override
    public ResponseData delete_role(int id) {
        ResponseData responseData = new ResponseData();
        int i = adminMapper.delete_role(id);
        if (i!=0){
            responseData.setCode(200);
            responseData.setMsg("删除成功");
        }else {
            responseData.setCode(507);
            responseData.setMsg("删除错误");
        }
        return responseData;
    }

    @Override
    public ResponseData delete_user(int id) {
        ResponseData responseData = new ResponseData();
        int i = adminMapper.delete_user(id);
        if (i!=0){
            responseData.setCode(200);
            responseData.setMsg("删除成功");
        }else {
            responseData.setCode(507);
            responseData.setMsg("删除错误");
        }
        return responseData;
    }

    @Override
    public ResponseData bu_men() {
        ResponseData responseData = new ResponseData();
        List<Dept> depts = adminMapper.bu_men();
        responseData.setCode(200);
        responseData.setMsg("成功");
        responseData.setData(depts);

        return responseData;
    }

    @Override
    public ResponseData fen_pei(String number, String buMenName) {
        ResponseData responseData = new ResponseData();
        int i = adminMapper.fen_pei(number, buMenName);
        if (i!=0){
            responseData.setCode(200);
            responseData.setMsg("成功");
        }else {
            responseData.setCode(508);
            responseData.setMsg("失败");
        }
        return responseData;
    }


    @Override
    public ResponseData allroleMenu() {
        List<Menu> menus = adminMapper.allroleMenu();
        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        responseData.setMsg("成功");
        responseData.setData(menus);
        return responseData;
    }


}
